package likong.bibliothek.pages.language;

import likong.bibliothek.obj.Language;

import org.apache.wicket.PageParameters;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.model.IModel;
import org.hibernate.Session;

import db.HibernateHelper;

public class LanguageEdit extends LanguageAbstract {
	public LanguageEdit(final PageParameters params) {
		super(params);
		// Load Person

		Session session = HibernateHelper.getSessionFactory()
				.getCurrentSession();
		session.beginTransaction();
		Object ret = session.get(Language.class, params.getInt("languageID"));
		if (ret == null) {
			// Fehler: Zurück zur Übersicht
			setRedirect(true);
			setResponsePage(LanguageWeb.class);
			return;
		}
		Language language = (Language) ret;

		// session.getTransaction()

		final CompoundPropertyModel<Language> model = new CompoundPropertyModel<Language>(
				language);

		// Panel einfügen
		add(new LanguageFormPanel("editLanguageFormular", model,
				"Language aktualisieren") {
			private static final long serialVersionUID = 1L;

			@Override
			protected void onSubmitModelHandle(IModel<Language> model) {
				Session session = HibernateHelper.getSessionFactory()
						.getCurrentSession();
				session.beginTransaction();
				session.saveOrUpdate(model.getObject());
				session.getTransaction().commit();
				// session.close();
				this.setResponsePage(LanguageWeb.class, params);
			}
		});

	}
}
